# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.3.1"

fastlane_require 'dotenv'
Dotenv.load '.env.secret'

default_platform :mac

platform :mac do
  lane :common_release do |options|
    build = "build"
    scheme = options[:scheme]
    app_name = "ONLYOFFICE"
    app = "#{build}/#{app_name}.app"
    git_suffix = options[:git_suffix]

    desc 'Cleanup'
    sh("rm -rf ../#{build}")

    desc 'Get the version from plist'
    version_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleShortVersionString"
    )
    build_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleVersion"
    )

    desc 'Build for developer id and notarize'
    gym(
      scheme: scheme,
      configuration: 'Release',
      clean: true,
      output_directory: build,
      codesigning_identity: ENV["CODESIGNING_IDENTITY"],
      export_method: 'developer-id',
      xcargs: "URL_WEBAPPS_HELP=https://download.onlyoffice.com/install/desktop/editors/help/v7.2.0-1/apps",
      skip_package_pkg: true
    )

    notarize(
      package: app,
      print_log: true,
      verbose: true
    )

    desc 'Prepare zip for delta update'
    sh("ditto -c -k --sequesterRsrc --keepParent #{ENV['PWD']}/#{app} #{ENV['PWD']}/#{build}/#{scheme}-#{version_number}.zip")

    desc 'Create DMG image'
    sh("npm install appdmg")
    sh("npx appdmg resources/appdmg.json ../#{build}/#{app_name}.dmg")

    if options[:skip_git_bump]
      next
    end

    desc 'Creates a bump version commit'
    commit_version_bump(
      message: "[macos] Version bumped to v#{version_number}(#{build_number})-#{git_suffix}",
      force: true
    )

    desc 'Create a local tag with the new version'
    add_git_tag(
      tag: "macos/v#{version_number}-#{build_number}-#{git_suffix}",
      build_number: build_number
    )

    # push changes
    push_to_git_remote
  end

  # arm64 only
  lane :release_arm do |options|
    desc 'Build Apple Silicone version'
    common_release(options.merge({ scheme: "ONLYOFFICE-arm", git_suffix: "arm" } ))
  end

  # x86_64 only
  lane :release_x86_64 do |options|
    desc 'Build Intel version'
    common_release(options.merge( { scheme: "ONLYOFFICE-x86_64", git_suffix: "x86_64" } ))
  end

  # x86_64 only with v8
  lane :release_v8 do |options|
    desc 'Build Intel version with v8 engine'
    common_release(options.merge( { scheme: "ONLYOFFICE-v8", git_suffix: "v8" } ))
  end

end
